#!/bin/sh
#Copyright (C) 2008-2009 Spark Zheng
#
#file:  reborn/mybackup.sh
#usage: ./mybackup.sh [parameters]
#desc:	backup my files (full backup or increasing backup)
#		for reborn.sh
#		for generate backup
#author:	Spark Zheng(mailto: mwishes AT gmail DOT com)
#history:
#           version 0.0.1   (initialized by Spark at 2008-12-31)
#

PATH=$PATH:/usr/lib/news/bin:/sbin:/usr/sbin:/usr/local/sbin
DEBUG=3                             #debug level: 0(none)/1/2/3(details)
LOG_FILE=/tmp/$0.${DEBUG}.log       #debug log file

DATE=$(date +%Y%m%d)
T_PWD=$(pwd)

declare -i POLICY

#backup tactics: 3(backup offline)/2(full)/1(increasing)/0(vif full)
POLICY=0
DIST_USER=zhengyu
DIST_HOME=/home/${DIST_USER}

DATA_DIR=/media/disk				#my u-disk root dir

VIF_FILE=./data/vif_zhengyu_${DATE}.tar.bz2              #vif file(out-put)
ALL_FULL_FILE=./data/all_full_zhengyu_${DATE}.tar.bz2    #all file full backup(out-put)
ALL_INCR_FILE=./data/all_incr_zhengyu_${DATE}.tar.bz2    #all file increasing backup(out-put)


#function area begin

#name: usage
function usage()
{
    echo "USAGE:$0 [option]"
	echo "      -p backup policy: could be 0(vif file backup),"
	echo "         1(all file full backup),2(all file increasing backup)"
	echo "         3(generate backup, sync data).  Default is 0"  
    echo "      -u user name of reborning system. Default: zhengyu"
	echo "      -d dist HOME dir. Default: /home/zhengyu"
	echo "      -D accumulating-file dir. Default is /media/disk"
	echo "      -h show this message"

    return 0
}

#name: backup_vif
#args: backup_vif dist_dir vif_file
function backup_vif()
{
    [ $DEBUG -ge 1 ] && echo "$0 backup_vif begin($1:$2) at $(date +%c)..." >> $LOG_FILE

    [ -e $1/.bash_rc ] && cp -f $1/.bash_rc files/bash_rc
    [ -e $1/.vimrc ] && cp -f $1/.vimrc files/vim_rc
    [ -d $1/.vim ] && tar -C $1 -vcjf files/vim_plugin.tar.bz2 .vim
    [ -e $1/.emacs ] && cp -f $1/.emacs files/emacs_rc
    [ -d $1/.ssh ] && tar -C $1 -vcjf files/ssh_rc.tar.bz2 .ssh
    [ -d $1/.subversion ] && tar -C $1 -vcjf files/subversion_rc.tar.bz2 .subversion

    [ $DEBUG -ge 1 ] && echo "$0 backup_vif finish at $(date +%c)." >> $LOG_FILE

    return 0
}

#name: backup_all_full
#args: backup_all_full dist_dir all_full_file
#
function backup_all_full()
{
    [ $DEBUG -ge 1 ] && echo "$0 backup_all_full begin($1:$2) at $(date +%c)..." >> $LOG_FILE

    tar -C $1 -vcjf $2 backup code cvs Documents Download ebook \
		english git Music Pictures Projects release research svn Videos Templates

    [ $DEBUG -ge 1 ] && echo "$0 backup_all_full finish at $(date +%c)." >> $LOG_FILE

    return 0
}

#name: backup_all_incr
#args: backup_all_incr dist_dir all_incr_file
function backup_all_incr()
{
    [ $DEBUG -ge 1 ] && echo "$0 backup_all_incr begin($1:$2) at $(date +%c)..." >> $LOG_FILE

	bunzip2 $2
	tar -C $1 -vujf ${2%.bz2} backup code cvs Documents Download ebook \
		english git Music Pictures Projects release research svn Videos Templates
	bzip2 ${2%.bz2}

    [ $DEBUG -ge 1 ] && echo "$0 backup_all_incr finish at $(date +%c)." >> $LOG_FILE

    return 0
}

#private functions
function _do_sync_data()
{
	rsync -au --delete --force --exclude="*~" --exclude=".*.swp" --exclude="#*#" \
		--exclude="lost+found" --exclude="Thumbs.db*" $1 $2
	return $?
}

function _do_sync_code()
{
	rsync -au --delete --force --exclude="*~" --exclude=".*.swp" --exclude="#*#" \
		--exclude="lost+found" --exclude="*.o" --exclude="*.pyc" $1 $2
	return $?
}

function _do_sync_blob()
{
	rsync -au --exclude="*~" --exclude=".*.swp" --exclude="#*#" --exclude="lost+found" \
		--exclude="Thumbs.db*" $1 $2
	return $?
}

function _do_sync_blob_delete()
{
	rsync -au --delete --exclude="*~" --exclude=".*.swp" --exclude="#*#" --exclude="lost+found" \
		--exclude="Thumbs.db*" $1 $2
	return $?
}

#name: sync_my_sorts
#args: sync_my_sorts home_dir data_dir
#		need root privillege
function sync_my_sorts()
{
    [ $DEBUG -ge 1 ] && echo "$0 sync_my_sorts begin($1:$2) at $(date +%c)..." >> $LOG_FILE

	#backups
	[ -d $1/.thunderbird ] && tar -C $1 -vcjf $2/backups/thunderbird_$(date +%Y%m%d)_0.tar.bz2 .thunderbird
	[ -d $1/.purple ] && tar -C $1 -vcjf $2/backups/pidgin_$(date +%Y%m%d)_0.tar.bz2 .purple
    [ -e $1/.bash_rc ] && cp -f $1/.bash_rc $2/backups/bash_rc
    [ -d $1/.vim ] && tar -C $1 -vcjf $2/backups/vim_plugin.tar.bz2 .vim
    [ -e $1/.vimrc ] && cp -f $1/.vimrc $2/backups/vim_rc
    [ -e $1/.emacs ] && cp -f $1/.emacs $2/backups/emacs_rc
    [ -d $1/.ssh ] && tar -C $1 -vcjf $2/backups/ssh_rc.tar.bz2 .ssh
    [ -d $1/.subversion ] && tar -C $1 -vcjf $2/backups/subversion_rc.tar.bz2 .subversion
	
	#my_code
	[ -d $1/code ] && _do_sync_code $1/code $2/my_code/
	[ -d $1/Projects ] && _do_sync_code $1/Projects $2/my_code/
	[ -d $1/release ] && _do_sync_code $1/release $2/my_code/
	[ -d $1/research ] && _do_sync_code $1/research $2/my_code/
	[ -d $1/source ] && _do_sync_code $1/source $2/my_code/
	[ -d $1/git ] && _do_sync_code $1/git $2/my_code/
	[ -d $1/svn ] && _do_sync_code $1/svn $2/my_code/
	[ -d $1/cvs ] && _do_sync_code $1/cvs $2/my_code/

	#my_doc
	[ -d $1/Documents ] && _do_sync_data $1/Documents $2/my_doc/
	[ -d $1/ebook ] && _do_sync_data $1/ebook $2/my_doc/
	[ -d $1/Templates -a "x$(ls ${1}/Templates)" != "x" ] && _do_sync_data $1/Templates $2/my_doc/

	#my_tips
	echo "my_tips Need to do manually..." >> $LOG_FILE

	#my_work
	echo "my_work Need to do manually..." >> $LOG_FILE

	#my_music
	[ -d $1/Music ] && _do_sync_data $1/Music $2/my_music/
	[ -d $1/english ] && _do_sync_data $1/english $2/my_music/
	
	#my_pic
	[ -d $1/Pictures -a "x$(ls ${1}/Pictures)" != "x" ] && _do_sync_data $1/Pictures/* $2/my_pic/
	
	#my_video
	[ -d $1/Videos -a "x$(ls ${1}/Videos)" != "x" ] && _do_sync_blob $1/Videos/* $2/my_video/
	
	#tools
	[ -d $1/Download ] && _do_sync_blob_delete $1/Download $2/tools/

    [ $DEBUG -ge 1 ] && echo "$0 sync_my_sorts finish at $(date +%c)." >> $LOG_FILE

	return 0
}

#function area end


#main begin
[ $DEBUG -ge 1 ] && echo "$0 begin at $(date +%c)..." >> $LOG_FILE

#getopt from cmdline
while getopts ":p:u:d:D:h" options
do
    case $options in
        p)  POLICY=$OPTARG
            ;;
        u)  DIST_USER=$OPTARG
            ;;
        d)  DIST_HOME=$OPTARG
            ;;
        D)  DATA_DIR=$OPTARG
            ;;
        h)  usage
            exit 255
            ;;
        *)  usage
            exit 254
            ;;
    esac
done

shift $(($OPTIND - 1))

[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: POLICY: $POLICY" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: user: $DIST_USER" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: home dir: $DIST_HOME" >> $LOG_FILE
[ $DEBUG -ge 3 ] && echo "$0 runtime parameters: accu-data-dir: $DATA_DIR" >> $LOG_FILE
[ $DEBUG -ge 3 -a "x$1" != "x" ] && echo "$0 runtime parameters: other: $1" >> $LOG_FILE

#do backup
if [ $POLICY -eq 0 ]
then
    backup_vif $DIST_HOME $VIF_FILE
fi

if [ $POLICY -eq 1 ]
then
    backup_all_full $DIST_HOME $ALL_FULL_FILE
fi

if [ $POLICY -eq 2 ]
then
    backup_all_incr $DIST_HOME $ALL_INCR_FILE
fi

if [ $POLICY -eq 3 ]
then
    sync_my_sorts $DIST_HOME $DATA_DIR
fi

[ $DEBUG -ge 1 ] && echo "$0 finish at $(date +%c)." >> $LOG_FILE
[ $DEBUG -ge 1 ] && echo "---------------------------------------" >> $LOG_FILE

exit 0
#main end
